package com.eedi.framework.template.controller.sys;

import com.eedi.framework.template.controller.sys.vo.role.*;
import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.template.controller.sys.vo.role.*;
import com.eedi.framework.template.dal.dataobject.SysTemplateRoleDO;
import com.eedi.framework.template.enums.SysTemplateRoleCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eedi.framework.template.service.SysTemplatePermissionService;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;

import static com.eedi.framework.common.pojo.CommonResult.success;
import static java.util.Collections.singleton;

@Tag(name = "系统界面 - 机构角色模版")
@RestController
@RequestMapping("/temp-role")
@Validated
public class SysTemplateRoleController {

    @Resource
    private SysTemplatePermissionService sysTemplatePermissionService;

    @PostMapping("/create")
    @Operation(summary = "创建角色")
    @PreAuthorize("@sys.hasPermission('sys:temp-role:create')")
    public CommonResult<String> createRole(@Valid @RequestBody SysTemplateRoleSaveReq createReqVO) {
        return success(sysTemplatePermissionService.createRole(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "修改角色")
    @PreAuthorize("@sys.hasPermission('sys:temp-role:update')")
    public CommonResult<Boolean> updateRole(@Valid @RequestBody SysTemplateRoleUpReq updateReqVO) {
        sysTemplatePermissionService.updateRole(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改角色状态")
    @PreAuthorize("@sys.hasPermission('sys:temp-role:update')")
    public CommonResult<Boolean> updateRoleStatus(@Valid @RequestBody SysTemplateRoleUpdateStatusReq reqVO) {
        sysTemplatePermissionService.updateRoleStatus(reqVO.getSysTemplateRoleId(), reqVO.getSysRoleStatus());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色")
    @Parameter(name = "sysTemplateRoleId", description = "角色编号", required = true, example = "1024")
    @PreAuthorize("@sys.hasPermission('sys:temp-role:delete')")
    public CommonResult<Boolean> deleteRole(@RequestParam("sysTemplateRoleId") String sysTemplateRoleId) {
        sysTemplatePermissionService.deleteRole(sysTemplateRoleId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得角色信息")
    @PreAuthorize("@sys.hasPermission('sys:temp-role:query')")
    public CommonResult<SysTemplateRoleResp> getRole(@RequestParam("sysTemplateRoleId") String sysTemplateRoleId) {
        SysTemplateRoleDO role = sysTemplatePermissionService.getRole(sysTemplateRoleId);
        return success(BeanUtils.toBean(role, SysTemplateRoleResp.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得角色分页")
    @PreAuthorize("@sys.hasPermission('sys:temp-role:query')")
    public CommonResult<PageResult<SysTemplateRoleResp>> getRolePage(SysTemplateRolePageReq pageReqVO) {
        pageReqVO.setSysRoleCode(SysTemplateRoleCodeEnum.DEFAULT.getCode());
        PageResult<SysTemplateRoleDO> pageResult = sysTemplatePermissionService.getRolePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SysTemplateRoleResp.class));
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取角色精简信息列表", description = "只包含被开启的角色，主要用于前端的下拉选项")
    public CommonResult<List<SysTemplateRoleSimpleResp>> getSimpleRoleList() {
        List<SysTemplateRoleDO> list = sysTemplatePermissionService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE));
        list.sort(Comparator.comparing(SysTemplateRoleDO::getSysRoleSort));
        List<SysTemplateRoleDO> filterList = list.stream().filter(x -> !SysTemplateRoleCodeEnum.SYS_ADMIN.getCode().equals(x.getSysRoleCode())).toList();
        return success(BeanUtils.toBean(filterList, SysTemplateRoleSimpleResp.class));
    }


}
